package com.estronger.shopmanage.ui.order.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.ui.order.until.Tools;
import com.estronger.shopmanage.ui.order.bean.OrderAttributeBean;
import com.estronger.shopmanage.ui.order.bean.TaocanshuxBean;
import com.estronger.shopmanage.utils.PrefUtils;
import com.estronger.shopmanage.utils.Toast;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;

/**
 * Created by DingYG on 2017-07-26.
 */

public class SelectHousesActivity extends BaseActivity implements View.OnClickListener {

    private View rl_root;
    private Activity mContext;
    private TextView tvfSTitle;
    private TextView tvfAddItem;
    private TextView tvfCCancle;
    private ListView listView;
    private RelativeLayout rlfNothing;
    private int whichShux;
    private ArrayList<OrderAttributeBean> attList;
    private ArrayList<TaocanshuxBean> shuList;

    @Override
    protected void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContext = SelectHousesActivity.this;
        rl_root = View.inflate(mContext, R.layout.activity_select_houses, null);
        setContentView(rl_root);
        setFinishOnTouchOutside(false);
        //设置Theme.Dialog View高度
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p = getWindow().getAttributes();
        p.height = (int) (d.getHeight() * 0.5);
        p.width = (int) (d.getWidth()); // 宽度设置为屏幕的0.7
        p.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(p);
    }

    @Override
    protected void initView() {
        tvfSTitle = (TextView) findViewById(R.id.tvfSelTitle);
        tvfAddItem = (TextView) findViewById(R.id.tvfClickToSure);
        tvfCCancle = (TextView) findViewById(R.id.tvfCCancle);
        listView = (ListView) findViewById(R.id.lvfHouses);
        rlfNothing = (RelativeLayout) findViewById(R.id.rlfNothing);
    }

    @Override
    protected void initAction() {
        tvfCCancle.setOnClickListener(this);
        try {
            try {
                Bundle bundle = getIntent().getExtras();
                whichShux = bundle.getInt(PrefUtils.WHITCHSHUXING, -1);
                if (whichShux == 1) {
                    tvfAddItem.setVisibility(View.VISIBLE);
                    tvfAddItem.setText("新增楼盘");
                    tvfAddItem.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            showEdit();
                        }
                    });
                } else if(whichShux == 6){
                    tvfAddItem.setVisibility(View.VISIBLE);
                    tvfAddItem.setText("新增来源");
                    tvfAddItem.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            showEdit2();
                        }
                    });
                }else if (whichShux == 8) {
                    tvfAddItem.setVisibility(View.INVISIBLE);
                    shuList = bundle.getParcelableArrayList(PrefUtils.BUNDLEARRAY);
                } else {
                    tvfAddItem.setVisibility(View.INVISIBLE);
                    attList = bundle.getParcelableArrayList(PrefUtils.BUNDLEARRAY);
                }

            } catch (Exception e) {
            }
            getRegionData(whichShux);
        } catch (Exception e) {
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvfCCancle:
                finish();
                break;
        }
    }

    /**
     * 加载数据
     */
    private void getRegionData(int type) {
        String tcid = "0";
        if (Tools.sTaocan != null) {
            tcid = Tools.sTaocan.getTcId();
        }
        switch (type) {
            case 1://楼盘
                tvfSTitle.setText("楼盘选择");
                RequestParams params0 = new RequestParams();
                params0.addBodyParameter("sf_action", "getbuildings");
                params0.addBodyParameter("usercode", MyApplication.getUserCode());
                params0.addBodyParameter("cityid", Tools.sCity.getAttId());
                params0.addBodyParameter("districtid", Tools.sCountry.getAttId());
                connect(params0, NetManager.ORDER_SELECT_HOUSES);
                break;
            case 2://系列
                tvfSTitle.setText("系列选择");
                Tools.showPopList(mContext, attList, listView, "SELECT_KITS");
                break;
            case 3://款式
                tvfSTitle.setText("款式选择");
                Tools.showPopList(mContext, attList, listView, "SELECT_STYLES");
                break;
            case 4://材质
                tvfSTitle.setText("树种选择");
                Tools.showPopList(mContext, attList, listView, "SELECT_RMQLTS");
                break;
            case 5://颜色
                tvfSTitle.setText("颜色选择");
                Tools.showPopList(mContext, attList, listView, "SELECT_COLOR");
                break;
            case 6://客户来源
                tvfSTitle.setText("来源选择");
                RequestParams params6 = new RequestParams();
                params6.addBodyParameter("sf_action", "getorigins");
                params6.addBodyParameter("usercode", MyApplication.getUserCode());
                connect(params6, NetManager.ORDER_SELECT_CUSTOMRES);
                break;
            case 8://套餐其它属性
                if (Tools.sOneTao != null) {
                    tvfSTitle.setText(Tools.sOneTao.getAttName());
                }
                Tools.showPopList_TcItem(mContext, shuList, listView);
                break;
        }
    }

    String strChange = "";
    String strChange1 = "";

    /**
     * 编辑对话框
     */
    public void showEdit() {
        final EditText e = new EditText(this);
        e.setBackgroundResource(R.drawable.red_round_white_kuang);

        View view = LayoutInflater.from(this).inflate(R.layout.view_dialog2,
                null);
        final Dialog dialog = new Dialog(this, R.style.dialog);
        dialog.setContentView(view);
        // 标题
        TextView tView = (TextView) view.findViewById(R.id.tvdialogtitle);
        // 弹窗口的内容
        LinearLayout tcontentLayout = (LinearLayout) view
                .findViewById(R.id.dialogcontent);
        Button okButton = (Button) view.findViewById(R.id.ok);
        Button cancelButton = (Button) view.findViewById(R.id.cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        okButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // 判断是否为空
                    strChange = e.getText().toString();
                    if (TextUtils.isEmpty(strChange)) {
                        Toast.show(mContext, "新增楼盘名字不能为空！", Toast.LENGTH_SHORT);
                        return;
                    }
                    //获取到需要传回的数据后 ，绑定在跳转的intent上
                    OrderAttributeBean result = new OrderAttributeBean();
                    result.setAttId(null);
                    result.setAttName(strChange);
                    Intent intent = new Intent(mContext, OrderEntryActivity.class);
                    intent.putExtra("SELECT_HOUSES", result);
                    setResult(Activity.RESULT_OK, intent);//Result_OK 表示结果响应成功
                    dialog.dismiss();
                    finish();
                }
            });
            tView.setText("请输入新增楼盘名称");
            e.setTextSize(14);
            e.setMaxEms(20);
            e.setLines(1);
            e.setFocusable(true);
            tcontentLayout.addView(e);
            dialog.show();
    }
    /**
     * 编辑对话框
     */
    public void showEdit2() {
        final EditText e = new EditText(this);
        e.setBackgroundResource(R.drawable.red_round_white_kuang);

        View view = LayoutInflater.from(this).inflate(R.layout.view_dialog2,
                null);
        final Dialog dialog = new Dialog(this, R.style.dialog);
        dialog.setContentView(view);
        // 标题
        TextView tView = (TextView) view.findViewById(R.id.tvdialogtitle);
        // 弹窗口的内容
        LinearLayout tcontentLayout = (LinearLayout) view
                .findViewById(R.id.dialogcontent);
        Button okButton = (Button) view.findViewById(R.id.ok);
        Button cancelButton = (Button) view.findViewById(R.id.cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // 判断是否为空
                strChange1 = e.getText().toString();
                if (TextUtils.isEmpty(strChange1)) {
                    Toast.show(mContext, "新增客户来源不能为空！", Toast.LENGTH_SHORT);
                    return;
                }
                //获取到需要传回的数据后 ，绑定在跳转的intent上
                OrderAttributeBean result = new OrderAttributeBean();
                result.setAttId(null);
                result.setAttName(strChange1);
                Intent intent = new Intent(mContext, OrderEntryActivity.class);
                intent.putExtra("SELECT_CUSTOMRES", result);
                setResult(Activity.RESULT_OK, intent);//Result_OK 表示结果响应成功
                dialog.dismiss();
                finish();
            }
        });
        tView.setText("请输入新增客户来源");
        e.setTextSize(14);
        e.setMaxEms(20);
        e.setLines(1);
        e.setFocusable(true);
        tcontentLayout.addView(e);
        dialog.show();
    }

    @Override
    protected void onNetSuccess(int code, String result) {
        super.onNetSuccess(code, result);
        if(code == NetManager.ORDER_SELECT_HOUSES){
            ArrayList<OrderAttributeBean> housesDatas = Tools.getResponseData(result, "buildid", "buildname");
            if (housesDatas == null || housesDatas.size() == 0) {
                Toast.show(mContext, "未获取到楼盘信息！", Toast.LENGTH_SHORT);
                listView.setVisibility(View.GONE);
                rlfNothing.setVisibility(View.VISIBLE);
            }else {
                rlfNothing.setVisibility(View.GONE);
                listView.setVisibility(View.VISIBLE);
                Tools.showPopList(mContext, housesDatas, listView, "SELECT_HOUSES");
            }
        } else if(code == NetManager.ORDER_SELECT_CUSTOMRES){
            ArrayList<OrderAttributeBean> cusResDatas = Tools.getResponseData(result, "id", "origindesc");
            if (cusResDatas == null || cusResDatas.size() == 0) {
                Toast.show(mContext, "未获取到客户来源信息！", Toast.LENGTH_SHORT);
                listView.setVisibility(View.GONE);
                rlfNothing.setVisibility(View.VISIBLE);
            }else {
                rlfNothing.setVisibility(View.GONE);
                listView.setVisibility(View.VISIBLE);
                Tools.showPopList(mContext, cusResDatas, listView, "SELECT_CUSTOMRES");
            }
        }
    }
}
